package com.reshare.controller;


import com.reshare.base.BaseController;
import com.reshare.bean.User;
import com.reshare.service.PermissionService;
import com.reshare.service.UserService;
import com.reshare.utils.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class IndexController extends BaseController {


    @Autowired
    private UserService userService;

    @Autowired
    private PermissionService permissionService;

    @RequestMapping("index")
    public String  index(HttpServletRequest req){
        //获取id
        Integer userId = LoginUserUtil.releaseUserIdFromCookie(req);
        //根据id获取用户信息
        User user = userService.selectByPrimaryKey(userId);
        //存储
        req.setAttribute("user",user);
        //返回页面
        return  "index";
    }

    @RequestMapping("welcome")
    public String  welcome(){
        return  "welcome";
    }

    @RequestMapping("main")
    public String  main(HttpServletRequest req){
        //获取id
        Integer userId = LoginUserUtil.releaseUserIdFromCookie(req);
        //根据id获取用户信息
        User user = userService.selectByPrimaryKey(userId);
        //存储
        req.setAttribute("user",user);
        List<String> permissions=permissionService.queryUserHasRolesHasPermissions(userId);
        req.getSession().setAttribute("permissions",permissions);
        //转页面
        return  "main";
    }


}
